home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Controls / Visual Basic Controls.iso / vbcontrol / sheriffa / slsdemo.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1999-05-19  |  10.4 KB  |  430 lines

  1. // SlsDemo.cpp : Defines the class behaviors for the application.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include "SlsDemo.h"
  6.  
  7. #include "MainFrm.h"
  8. #include "SlsDemoDoc.h"
  9. #include "SlsDemoView.h"
  10. #include "ExportDlg.h"
  11.  
  12. #include "SlsApi.h"
  13.  
  14. #include "RegisterDlg.h"
  15. #include "RenewDlg.h"
  16. #include "ExceedDlg.h"
  17. #include "LicenceDlg.h"
  18.  
  19. #ifdef _DEBUG
  20. #define new DEBUG_NEW
  21. #undef THIS_FILE
  22. static char THIS_FILE[] = __FILE__;
  23. #endif
  24.  
  25. /////////////////////////////////////////////////////////////////////////////
  26. // CSlsDemoApp
  27.  
  28. char g_szProductID[]={"9758-3050-1918-9292-6466"};
  29. SLS_SECRET g_arySecrets[]=
  30. {
  31.     {0x76,0x3E,0x6D,0x36,0x14,0x14,0x6D,0xB8,0x1D,0xCA,0xF3,0xD5,0x34,0x2D,0x08,0xBC,0x14,0x31},
  32.     {0x00,0xB7,0x17,0x00,0x43,0xFC,0xC0,0x7F,0x8F,0xA2,0x22,0xD2,0x1B,0x62,0xB3,0xDE,0x95,0x3F},
  33.     {0xAC,0x2E,0x39,0x6F,0x45,0xD5,0x41,0x11,0x43,0x41,0x2B,0x75,0x9C,0xB2,0x67,0x6E,0x14,0x02},
  34.     {0x79,0x52,0x9A,0x49,0x97,0x8B,0x1C,0x15,0x64,0xB7,0xBE,0x6C,0x7F,0x18,0xC3,0x4E,0x78,0x87},
  35. };
  36.  
  37. /*
  38. SLS_SECRET g_arySecrets[4]=
  39. {
  40.     {"5267-4088-9823-3212"},
  41.     {"3166-1346-2340-4525"},
  42.     {"2156-4924-7515-9864"},
  43.     {"2056-0703-9264-0145"},        
  44. };
  45. */
  46.  
  47. BEGIN_MESSAGE_MAP(CSlsDemoApp, CWinApp)
  48.     //{{AFX_MSG_MAP(CSlsDemoApp)
  49.     ON_COMMAND(ID_APP_ABOUT, OnAppAbout)
  50.     ON_COMMAND(ID_TEST_EXPORT, OnTestExport)
  51.     ON_COMMAND(ID_TEST_QUERYUSERINFO, OnTestQueryuserinfo)
  52.     //}}AFX_MSG_MAP
  53.     // Standard file based document commands
  54.     ON_COMMAND(ID_FILE_NEW, CWinApp::OnFileNew)
  55.     ON_COMMAND(ID_FILE_OPEN, CWinApp::OnFileOpen)
  56.     // Standard print setup command
  57.     ON_COMMAND(ID_FILE_PRINT_SETUP, CWinApp::OnFilePrintSetup)
  58. END_MESSAGE_MAP()
  59.  
  60. /////////////////////////////////////////////////////////////////////////////
  61. // CSlsDemoApp construction
  62.  
  63. CSlsDemoApp::CSlsDemoApp()
  64. {
  65.     // TODO: add construction code here,
  66.     // Place all significant initialization in InitInstance
  67.     m_pSheriff=NULL;
  68.     m_bRunInDemoMode=FALSE;
  69.     m_bLicenceOK=FALSE;
  70.     m_nAccessKey=0;
  71. }
  72.  
  73. CSlsDemoApp::~CSlsDemoApp()
  74. {
  75.     delete m_pSheriff;
  76. }
  77. /////////////////////////////////////////////////////////////////////////////
  78. // The one and only CSlsDemoApp object
  79.  
  80. CSlsDemoApp theApp;
  81.  
  82. /////////////////////////////////////////////////////////////////////////////
  83. // CSlsDemoApp initialization
  84.  
  85. BOOL CSlsDemoApp::InitInstance()
  86. {
  87.     // Standard initialization
  88.     // If you are not using these features and wish to reduce the size
  89.     //  of your final executable, you should remove from the following
  90.     //  the specific initialization routines you do not need.
  91.  
  92. #ifdef _AFXDLL
  93.     Enable3dControls();            // Call this when using MFC in a shared DLL
  94. #else
  95.     Enable3dControlsStatic();    // Call this when linking to MFC statically
  96. #endif
  97.  
  98.     // Change the registry key under which our settings are stored.
  99.     // You should modify this string to be something appropriate
  100.     // such as the name of your company or organization.
  101.     SetRegistryKey(_T("Sheriff Demo"));
  102.  
  103.     LoadStdProfileSettings();  // Load standard INI file options (including MRU)
  104.  
  105.     // Register the application's document templates.  Document templates
  106.     //  serve as the connection between documents, frame windows and views.
  107.  
  108.     CSingleDocTemplate* pDocTemplate;
  109.     pDocTemplate = new CSingleDocTemplate(
  110.         IDR_MAINFRAME,
  111.         RUNTIME_CLASS(CSlsDemoDoc),
  112.         RUNTIME_CLASS(CMainFrame),       // main SDI frame window
  113.         RUNTIME_CLASS(CSlsDemoView));
  114.     AddDocTemplate(pDocTemplate);
  115.  
  116.     // Parse command line for standard shell commands, DDE, file open
  117.     CCommandLineInfo cmdInfo;
  118.     ParseCommandLine(cmdInfo);
  119.     m_strUserName=cmdInfo.m_strFileName;
  120.     if(m_strUserName.IsEmpty())
  121.         m_strUserName="Sheriff Demo";
  122.  
  123.     m_pSheriff=new CSheriff(g_szProductID,m_strUserName);
  124.     if(!m_pSheriff->Succeeded())
  125.     {
  126.         ShowSheriffError();
  127.         return FALSE;
  128.     }
  129.     m_pSheriff->SetSecrets(g_arySecrets);
  130.  
  131.     if(!m_pSheriff->IsProductLicensed())
  132.     {
  133.         RegisterProduct();
  134.  
  135.         //If you want to automaticlly issue a trial licence 
  136.         //eanble the following code
  137.  
  138.         /* ------------------------------------------------------ 
  139.         //Issue a 30-day, single-user, standalone trial licence
  140.         SLS_LICENCE TrialLicence;
  141.         memset(&TrialLicence,0,sizeof(SLS_LICENCE));
  142.         TrialLicence.Type = SLS_TYPE_TIME_METER|SLS_TYPE_CONCURRENCY|SLS_TYPE_STANDALONE;
  143.         TrialLicence.Meter=30;    //30 day
  144.         TrialLicence.CoUsers=1;    //1 user only
  145.         if(!m_pSheriff->License(TrialLicence))
  146.         {
  147.             ShowSheriffError();
  148.             return FALSE;
  149.         }
  150.         ---------------------------------------------------------*/
  151.     }
  152.  
  153.     if (!CheckLicence())
  154.         return FALSE;
  155.  
  156.     // create main MDI Frame window
  157.     CMainFrame* pMainFrame = new CMainFrame;
  158.     if (!pMainFrame->LoadFrame(IDR_MAINFRAME))
  159.         return FALSE;
  160.     m_pMainWnd = pMainFrame;
  161.  
  162.     // The one and only window has been initialized, so show and update it.
  163.     m_pMainWnd->ShowWindow(SW_SHOW);
  164.     m_pMainWnd->UpdateWindow();
  165.  
  166.     return TRUE;
  167. }
  168.  
  169. BOOL CSlsDemoApp::ExitInstance()
  170. {
  171.     if(!m_bRunInDemoMode)
  172.     {
  173.         SLS_RELEASE Release;
  174.         memset(&Release,0,sizeof(Release));
  175.         Release.UnitsConsumed=0L;
  176.         m_pSheriff->Release(Release);
  177.     }
  178.     delete m_pSheriff;
  179.     m_pSheriff=NULL;
  180.     return CWinApp::ExitInstance();
  181. }
  182.  
  183. /////////////////////////////////////////////////////////////////////////////
  184. // CAboutDlg dialog used for App About
  185.  
  186. class CAboutDlg : public CDialog
  187. {
  188. public:
  189.     CAboutDlg();
  190.  
  191. // Dialog Data
  192.     //{{AFX_DATA(CAboutDlg)
  193.     enum { IDD = IDD_ABOUTBOX };
  194.     //}}AFX_DATA
  195.  
  196.     // ClassWizard generated virtual function overrides
  197.     //{{AFX_VIRTUAL(CAboutDlg)
  198.     protected:
  199.     virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
  200.     //}}AFX_VIRTUAL
  201.  
  202. // Implementation
  203. protected:
  204.     //{{AFX_MSG(CAboutDlg)
  205.         // No message handlers
  206.     //}}AFX_MSG
  207.     DECLARE_MESSAGE_MAP()
  208. };
  209.  
  210. CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
  211. {
  212.     //{{AFX_DATA_INIT(CAboutDlg)
  213.     //}}AFX_DATA_INIT
  214. }
  215.  
  216. void CAboutDlg::DoDataExchange(CDataExchange* pDX)
  217. {
  218.     CDialog::DoDataExchange(pDX);
  219.     //{{AFX_DATA_MAP(CAboutDlg)
  220.     //}}AFX_DATA_MAP
  221. }
  222.  
  223. BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
  224.     //{{AFX_MSG_MAP(CAboutDlg)
  225.         // No message handlers
  226.     //}}AFX_MSG_MAP
  227. END_MESSAGE_MAP()
  228.  
  229. // App command to run the dialog
  230. void CSlsDemoApp::OnAppAbout()
  231. {
  232.     CAboutDlg aboutDlg;
  233.     aboutDlg.DoModal();
  234. }
  235.  
  236. /////////////////////////////////////////////////////////////////////////////
  237. // CSlsDemoApp commands
  238.  
  239. BOOL CSlsDemoApp::CheckLicence()
  240. {
  241.     SLS_REQUEST Request;
  242.     SLS_PERMIT    Permit;
  243.     memset(&Request,0,sizeof(Request));
  244.     Request.UnitsReserved=0L;
  245.     if(m_pSheriff->Request(Request,Permit))
  246.     {
  247.         SLS_LICENCE_INFO LicenceInfo;
  248.         m_pSheriff->QueryLicenceInfo(LicenceInfo);
  249.  
  250.         SLS_OPTIONS Options;
  251.         Options.HeartbeatTime=2;
  252.         Options.ReclaimTime=3;
  253.         Options.Reserved1=0;
  254.         Options.Reserved2=0;
  255.         m_pSheriff->SetOptions(Options);
  256.  
  257.         m_bLicenceOK=TRUE;
  258.         m_nAccessKey=Permit.AccessKey;
  259.         return TRUE;
  260.     }
  261.     //Request Failure
  262.     BOOL bRegister=FALSE;
  263.     //Register product
  264.     if(m_pSheriff->GetLastError()==SLS_E_LICENCE_UNREGISTERED)
  265.     {
  266.         bRegister=RegisterProduct();
  267.     }
  268.     
  269.     if(m_pSheriff->GetLastError()==SLS_E_LICENCE_UNDEFINED || bRegister)
  270.     {
  271.         //Licence not registered yet
  272.         CRegisterDlg dlgRegister;
  273.         int nRet=dlgRegister.DoModal();
  274.         if(nRet==IDC_REGISTER)
  275.             return RegisterLicence();
  276.         if(nRet==IDC_EXIT)
  277.             return FALSE;
  278.         if(nRet==IDC_CONTINUE)
  279.         {
  280.             RunInDemoMode();
  281.             return TRUE;
  282.         }
  283.     }
  284.     else
  285.     if(m_pSheriff->GetLastError()==SLS_E_LICENCE_EXPIRED)
  286.     {
  287.         //Licence has expired
  288.         CRenewDlg dlgRenew;
  289.         int nRet=dlgRenew.DoModal();
  290.         if(nRet==IDC_RENEW)
  291.             return RegisterLicence();
  292.         if(nRet==IDC_EXIT)
  293.             return FALSE;
  294.         if(nRet==IDC_CONTINUE)
  295.         {
  296.             RunInDemoMode();
  297.             return TRUE;
  298.         }
  299.     }
  300.     else
  301.     if(m_pSheriff->GetLastError()==SLS_E_LICENCE_EXCEEDED)
  302.     {
  303.         //Too many users
  304.         CExceedDlg dlgExceed;
  305.         int nRet=dlgExceed.DoModal();
  306.         if(nRet==IDC_EXIT)
  307.             return FALSE;
  308.         if(nRet==IDC_CONTINUE)
  309.         {
  310.             RunInDemoMode();
  311.             return TRUE;
  312.         }
  313.         return FALSE;
  314.     }
  315.  
  316.     //Other errors
  317.     ShowSheriffError();
  318.     return FALSE;
  319. }
  320.  
  321. BOOL CSlsDemoApp::RegisterProduct()
  322. {
  323.     //Register product
  324.       char szModuleName[257],*p;     
  325.       HINSTANCE hinst  = AfxGetInstanceHandle() ;   
  326.       GetModuleFileName(hinst,szModuleName,sizeof(szModuleName)-1);
  327.       if(p=strrchr(szModuleName,'\\')) *p=0;
  328.     if(!m_pSheriff->Register("Sheriff Demo",szModuleName))
  329.     {
  330.         ShowSheriffError();
  331.         return FALSE;
  332.     }
  333.  
  334.     return TRUE;
  335. }
  336.  
  337. BOOL CSlsDemoApp::RegisterLicence()
  338. {
  339.     //Get Reference Code
  340.     CString strReference;
  341.     if(!m_pSheriff->GetReference(strReference))
  342.     {
  343.         ShowSheriffError();
  344.         return FALSE;
  345.     }
  346.  
  347.     CLicenceDlg    dlgLicence;
  348.     dlgLicence.m_strReferenceCode=strReference;
  349.     if(dlgLicence.DoModal()==IDOK)
  350.     {
  351.         if(!m_pSheriff->SetLicence(dlgLicence.m_strReferenceCode,dlgLicence.m_strLicenceKey))
  352.         {
  353.             ShowSheriffError();
  354.             return FALSE;
  355.         }
  356.  
  357.         SLS_REQUEST Request;
  358.         SLS_PERMIT    Permit;
  359.         memset(&Request,0,sizeof(Request));
  360.         Request.UnitsReserved=0L;
  361.         if(!m_pSheriff->Request(Request,Permit))
  362.         {
  363.             ShowSheriffError();
  364.             return FALSE;
  365.         }
  366.  
  367.         m_bLicenceOK=TRUE;
  368.         m_nAccessKey=Permit.AccessKey;
  369.         return TRUE;
  370.     }
  371.     return FALSE;
  372. }
  373.  
  374. void CSlsDemoApp::ShowSheriffError()
  375. {
  376.     CString strError;
  377.     m_pSheriff->GetLastErrorMessage(strError);
  378.     AfxMessageBox(strError,MB_OK|MB_ICONSTOP);
  379. }
  380.  
  381. void CSlsDemoApp::OnTestExport() 
  382. {
  383.     CExportDlg dlg;
  384.     if(dlg.DoModal() == IDOK)
  385.     {
  386.         //We must release our current licence handle before export
  387.         //We'll request again once finish exporting
  388.         SLS_RELEASE Release;
  389.         memset(&Release,0,sizeof(Release));
  390.         Release.UnitsConsumed=0L;
  391.         m_pSheriff->Release(Release);
  392.  
  393.         //for simplicity, the export parameters are fixed in the demo
  394.         //you may want to provide options in the Export Dialog
  395.         //for users to select, like what SLSADMIN does
  396.         SLS_LICENCE ExportLicence;
  397.         memset(&ExportLicence,0,sizeof(SLS_LICENCE));
  398.         ExportLicence.Type = SLS_TYPE_TIME_METER|SLS_TYPE_CONCURRENCY|SLS_TYPE_STANDALONE;
  399.         ExportLicence.Meter=10;        //10 day
  400.         ExportLicence.CoUsers=1;    //1 user only
  401.  
  402.         CString strExportLicenceKey;
  403.         if(!m_pSheriff->Export(ExportLicence,dlg.m_strReferenceCode,strExportLicenceKey))
  404.         {
  405.             ShowSheriffError();
  406.             return;
  407.         }
  408.     
  409.         MessageBox(NULL,strExportLicenceKey,"Export Key",MB_OK);
  410.  
  411.         //We must release the
  412.         if(!CheckLicence())
  413.         {
  414.             //check licence failed, you might want to exit your application now
  415.             //exit
  416.         }
  417.     }    
  418. }
  419.  
  420.  
  421. void CSlsDemoApp::OnTestQueryuserinfo() 
  422. {
  423.     DWORD dwUserCount = m_pSheriff->GetUserCount();
  424.     for(DWORD i=1;i<=dwUserCount;i++)
  425.     {
  426.         SLS_USER_INFO UserInfo;
  427.         m_pSheriff->QueryUserInfo(i,UserInfo);
  428.     }
  429. }
  430.